package com.lele.file;

import java.io.IOException;

import com.lele.util.FolderUtil;

public class SpringConfigFile extends SuperFile {
	public SpringConfigFile(SuperFile sf) {
		this.project_root = sf.getProject_root();
		this.file_url = sf.getFile_url().substring(0,
				sf.getFile_url().indexOf("src") + 3);
		this.entityClass = sf.getEntityClass();
		this.entityName = sf.getEntityName();
		this.package_url = sf.getPackage_url();
	}

	public SpringConfigFile() {

	}

	public String[] getConfigURL() {
		String[] urls = new String[2];

		StringBuilder actions_url = new StringBuilder();
		StringBuilder beans_url = new StringBuilder();

		actions_url = actions_url.append(file_url).append(
				"\\applicationContext-actions.xml");
		beans_url = beans_url.append(file_url).append(
				"\\applicationContext-beans.xml");

		urls[0] = actions_url.toString();
		urls[1] = beans_url.toString();

		return urls;
	}

	public String getActionsContent() throws IOException {
		StringBuilder new_actions = new StringBuilder();
		new_actions.append(FolderUtil.readTxt(
				this.getConfigURL()[0].toString(), "UTF-8"));
		if (new_actions.toString().equals("")) {
			String build = System.getProperty("user.dir")
					+ "\\src\\template\\application-template.xml";
			new_actions.append(FolderUtil.readTxt(build.toString(), "UTF-8"));
		}
		if (new_actions.indexOf(entityClass + "Action") < 0) {
			StringBuilder actions_str = new StringBuilder();
			actions_str = actions_str
					.append("    <bean name=\"")
					.append(entityName)
					.append("Action\" class=\"")
					.append(package_url.replace("\\", "."))
					.append(".action.")
					.append(entityName)
					.append(".")
					.append(entityClass)
					.append("Action\" autowire=\"byName\"  scope=\"prototype\">");
			actions_str.append("\r\n");
			actions_str.append("    </bean>");
			actions_str.append("\r\n");

			new_actions.insert(new_actions.indexOf("</beans>"),
					actions_str.toString());
		}
		return new_actions.toString();
	}

	public String getBeansContent() throws IOException {
		StringBuilder new_beans = new StringBuilder();
		new_beans = new_beans.append(FolderUtil.readTxt(
				this.getConfigURL()[1].toString(), "UTF-8"));

		if (new_beans.toString().equals("")) {
			String build = System.getProperty("user.dir")
					+ "\\src\\template\\application-template.xml";
			new_beans.append(FolderUtil.readTxt(build.toString(), "UTF-8"));
		}
		if (new_beans.indexOf(entityClass + "Service") < 0) {
			StringBuilder beans_str = new StringBuilder();
			beans_str = beans_str.append("    <bean id=\"").append(entityName)
					.append("DAO\" class=\"")
					.append(package_url.replace("\\", ".")).append(".daoimpl.")
					.append(entityName).append(".").append(entityClass)
					.append("DAO\">");
			beans_str.append("\r\n");
			beans_str.append("        <constructor-arg>");
			beans_str.append("\r\n");
			beans_str.append("            <value>")
					.append(package_url.replace("\\", ".")).append(".entity.")
					.append(entityClass).append("</value>");
			beans_str.append("\r\n");
			beans_str.append("        </constructor-arg>");
			beans_str.append("\r\n");
			beans_str
					.append("        <property name=\"sessionFactory\" ref=\"sessionFactory\"></property>");
			beans_str.append("\r\n");
			beans_str.append("    </bean>");
			beans_str.append("\r\n");
			beans_str.append("    <bean id=\"").append(entityName)
					.append("Service\" class=\"")
					.append(package_url.replace("\\", "."))
					.append(".serviceimpl.").append(entityName).append(".")
					.append(entityClass)
					.append("Service\" autowire=\"byName\"></bean>");
			beans_str.append("\r\n");
			beans_str.append("\r\n");

			new_beans.insert(new_beans.indexOf("</beans>"),
					beans_str.toString());
		}
		return new_beans.toString();
	}
}
